// packageI/lawyerPlatform/chat/chat.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    last_page:1,
    page:1,
    message_type:"",
    l_id:"",
    timerId:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:  function (options){ 
    if(options.l_id){
      this.setData({
        l_id:options.l_id
      })
    }
    this.setData({
      message_type:options.message_type
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getMessageDate(timestamp){
    let date = new Date(timestamp*1000);
    let year = date.getFullYear(),
        month = date.getMonth()+1,
        day = date.getDate();
    if(month<10){
      month=`0${month}`
    }
    if(day<10){
      day=`0${day}`
    }
    return `${year}-${month}-${day}`;
  },
  getData(){
    console.log("请求聊天消息")
   let  json = {
     message_type: this.data.message_type
    }
    if(this.data.l_id){
      json.l_id = this.data.l_id;
    }
    app._postNetWork({
      url:app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.message.getList"),
      data:json,
      success:res => {
        if(res.data.result == 1){
          res.data.data.list.data.forEach(item => {
            item.last_msg_time = this.getMessageDate(item.last_msg_time)
          });
          this.setData({
            list:res.data.data.list.data,
            last_page:res.data.data.list.last_page
          })
        }
      }
    })
  },
  link(e){
    let data = e.currentTarget.dataset.item;
    // user_id客服
    let json = {};
    if(this.data.message_type == 2 || this.data.message_type == 3){
      json.chatType = 2;
    }else{
      json.chatType = 1;
      json.user_id = data.id;
    }
    json.message_type = this.data.message_type;
    json.plugin_name = "lawyer";
    json.employeeId =data.employee_id

    clearInterval( this.data.timerId)
    this.data.timerId = null;
    if(this.data.message_type == 2 ||this.data.message_type == 3 ){
      wx.navigateTo({
        url: '/packageH/chitchat/chatWindow/chatWindow?chatType=2&user_id='+data.uid+'&employeeId=' + data.employee_id+'&plugin_name=lawyer&message_type='+this.data.message_type+'&queue_id='+data.id,
      })
    }else{
      wx.navigateTo({
        url: '/packageH/chitchat/chatWindow/chatWindow?chatType=1&user_id=' + wx.getStorageSync('uid') + '&employeeId='+data.employee_id+'&plugin_name=lawyer&message_type=1',
      });
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
    await this.getData()
    this.data.timerId = setInterval(()=>{
      this.getData()
    },5000)

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.page>=this.data.last_page){
        return false;
    }else{
      let  json = {
        message_type: this.data.message_type
       }
       if(this.data.l_id){
         json.l_id = this.data.l_id;
       }
       app._postNetWork({
         url:app.getNetAddresss("plugin.lawyer-platform.frontend.controllers.message.getList"),
         data:json,
         success:res => {
           if(res.data.result == 1){
             res.data.data.list.data.forEach(item => {
               item.last_msg_time = this.getMessageDate(item.last_msg_time)
             });
             this.data.list = this.data.list.concat(res.data.data.list.data)
             this.setData({
               list: this.data.list,
               page:res.data.data.list.current_page,
               last_page:res.data.data.list.last_page
             })
           }
         }
       })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})